package com.yonyou.pmclouds.workstopreport.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.*;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.picture.entity.PictureParamVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import com.yonyou.pmclouds.workstopreport.entity.WorkStopVO;
import com.yonyou.pmclouds.workstopreport.mapper.WorkStopMapper;
import com.yonyou.pmclouds.workstopreport.process.PreWorkResumeDateWriteBackProjectProcessor;
import com.yonyou.pmclouds.workstopreport.process.WorkStopCopyPersonInsertBatchProcessor;
import com.yonyou.pmclouds.workstopreport.process.WorkStopInsertCheckProcessor;
import com.yonyou.pmclouds.workstopreport.process.WorkStopProjectLogPostProcess;
import com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopMaintain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = com.yonyou.pmclouds.workstopreport.rmiitf.WorkStopMaintain.class )
public class WorkStopMaintainImpl implements WorkStopMaintain, IFlowApproveBuziService {

    @Autowired
    private WorkStopMapper mapper;
    @Autowired
    private PictureRemoteService pictureService;
    @Autowired
    private ProjectStatusCheckProcessor<WorkStopVO> projectStatusCheckProcessor;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<WorkStopVO> billCountFactory;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<WorkStopVO> billCountDeleteFactory;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<WorkStopVO> billForUpdateProcessorFactory;
    @Autowired
    private BillDrivenProcessorFactory drivenProcessorFactory;
    @Autowired
    private WorkStopProjectLogPostProcess workStopProjectLogPostProcess;
    @Autowired
    private WorkStopCopyPersonInsertBatchProcessor workStopCopyPersonInsertBatchProcessor;
    @Autowired
    private PreWorkResumeDateWriteBackProjectProcessor preWorkResumeDateWriteBackProjectProcessor;
    @Autowired
    private MobileNoticeInsertProcessor<WorkStopVO> mobileNoticeInsertProcessor;//抄送人发通知
    @Autowired
    private MobileTaskInsertProcessor<WorkStopVO> taskInsertProcessor;//待办

    //更新状态字段
    private static String[] UPDATE_STATUS = {WorkStopVO.BILLSTATUS};

    //主表需要更新的字段名
    private static String[] UPDATE_FIELDS = {WorkStopVO.PROJECT_NAME,WorkStopVO.PROJECT_CODE,WorkStopVO.STOPDATE,WorkStopVO.PRERESUMEDATE,WorkStopVO.WORKSTOPDAYS};

    @PMApprove
    @Override
    public WorkStopVO insert(WorkStopVO workStopVO) throws BusinessException {
        workStopVO.setOperation(WorkStopVO.OPERATION_ADD);
        //创建一个单实体得插入模板对象
        SingleInsertBpTemplate<WorkStopVO> bp = new SingleInsertBpTemplate<>(mapper);
        //补全字段创建者以及创建时间
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<WorkStopVO>());
        //判断是否为空
        bp.addBeforeProcessor(new WorkStopInsertCheckProcessor());
        //结束的项目无法添加
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //更新单据数量统计
        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        //智能日志
        bp.addAfterProcessor(workStopProjectLogPostProcess);
        //批量插入抄送人
        bp.addAfterProcessor(workStopCopyPersonInsertBatchProcessor);
        //添加单据，统一调用process方法
        bp.insert(workStopVO);
        //存储图片和小视频
        PictureParamVO paramVO = new PictureParamVO(workStopVO.getPkWorkstopreport(), workStopVO.getPkProject(), BillTypeConst.WORKSTOP_BILLTYPE);
        pictureService.insertPictureForPub(workStopVO, paramVO);
        return workStopVO;
    }

    @PMApprove(trigger = "commit")
    @Override
    public WorkStopVO update(WorkStopVO workStopVO) throws BusinessException {
        //创建一个单个实体更新模板对象，传入你要更新的实体的mapper对象以及你想要更新或者说可以更新的字段名。这样在更新时会自动只更新这些字段。
        SingleUpdateBpTemplate<WorkStopVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
        //添加修改者以及修改时间
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<WorkStopVO>());
        //检验项目状态，判断项目是否已经竣工
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //更新统计单据，主要更新项目单据统计单据状态
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        //更新主表数据
        bp.update(workStopVO);
        return workStopVO;
    }

    @Override
    public WorkStopVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        if (StringUtils.isEmpty(pk_bill)) {
            return null;
        }
        WorkStopVO workStopVO = mapper.getById(pk_bill);
        workStopVO.setOperation(WorkStopVO.OPERATION_PASS);
        SingleUpdateBpTemplate<WorkStopVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);

        workStopVO.setBillstatus(billStatus);

        // 单据驱动项目状态变化(并把预计复工日期回写到NCC中)
        //bp.addAfterProcessor(drivenProcessorFactory.getBillDrivenProcessor(BillTypeConst.WORKSTOP_BILLTYPE, (BillDrivenProcessorFactory.AdditionalInfoToDriven<WorkStopVO>) null));

        // 回写预计复工日期(和停工日期,为计算项目停工天数准备，业务上没有要求)
        bp.addAfterProcessor(preWorkResumeDateWriteBackProjectProcessor);
        // 给抄送人发送通知
        bp.addAfterProcessor(mobileNoticeInsertProcessor);

        bp.update(workStopVO);

        return workStopVO;
    }

    @Override
    public WorkStopVO rejectByPkBill(String pk_bill) throws BusinessException {
        if(StringUtils.isEmpty(pk_bill)){
            return null;
        }
        //根据单据主键获得停工单实体
        WorkStopVO workStopVO = mapper.selectReport(pk_bill);
        workStopVO.setOperation(WorkStopVO.OPERATION_RECTIFY);
        //告诉更改模板要更改的字段
        SingleUpdateBpTemplate<WorkStopVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        //将实体的状态改变
        //workStopVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        //更新统计单据，主要更新项目单据统计单据状态
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        bp.update(workStopVO);
        return workStopVO;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<WorkStopVO> bp = new SingleDeleteBpTemplate<>(mapper);
        bp.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        return bp.delete((WorkStopVO) businessVO);
    }

    @Override
    public void delete(WorkStopVO workStopVO) throws BusinessException {
        SingleDeleteBpTemplate<WorkStopVO> bp = new SingleDeleteBpTemplate<>(mapper);
        //更新单据
        bp.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.WORKSTOP_BILLTYPE));
        bp.delete(workStopVO);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        return mapper.selectReport(pk_bill);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.WORKSTOP_BILLTYPE;
    }
}
